package com.wangshen.base.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.wangshen.base.R;


/**
 * 作者:zhuliangliang
 * 创建时间: 2018/9/10
 *
 */
public class MyAutoDialogUtil {

    private static MyClickListener mListener;

    //自定义接口，用于回调按钮点击事件到Activity
    public interface MyClickListener{
        public void clickListener(View v);
    }


    public static void showDialog(final Activity context,String title, boolean bool, String dialog_content){

        View view = LayoutInflater.from(context).inflate(R.layout.dialog_log_out, null);
        final Dialog dialog = new Dialog(context, R.style.dialog);
        TextView txtContent = view.findViewById(R.id.txt_content);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        Button btnConfirm = view.findViewById(R.id.btn_confirm);
        ImageView imgError = view.findViewById(R.id.img_error);

        txtContent.setText(dialog_content);
        tvTitle.setText(title);
        if(bool == true){
            imgError.setVisibility(View.VISIBLE);
        }else{
            imgError.setVisibility(View.GONE);
        }


        btnConfirm.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });

        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        @SuppressWarnings("deprecation")
        int width = (int) (SystemUtils.getScreenWidth(context) * 0.8);
        lp.width = width;
        dialog.getWindow().setAttributes(lp);
        dialog.show();
    }

    public static void showDialog(final Activity context, String dialog_content,MyClickListener listener){
        mListener = listener;
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_log_out1, null);
        final Dialog dialog = new Dialog(context, R.style.dialog);
        TextView txt_content = view.findViewById(R.id.txt_content);
        txt_content.setText(dialog_content);
        Button btn_confirm = view.findViewById(R.id.btn_confirm);
        Button btn_cancel = view.findViewById(R.id.btn_cancel);

        btn_confirm.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mListener.clickListener(v);
                dialog.dismiss();
            }
        });

        btn_cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        @SuppressWarnings("deprecation")
        int width = (int) (SystemUtils.getScreenWidth(context) * 0.8);
        lp.width = width;
        dialog.getWindow().setAttributes(lp);
        dialog.show();
    }

    public static void showDialog(final Activity context,String title, String dialog_content){

        View view = LayoutInflater.from(context).inflate(R.layout.dialog_log_out, null);
        final Dialog dialog = new Dialog(context, R.style.dialog);
        TextView txtContent = view.findViewById(R.id.txt_content);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        Button btnConfirm = view.findViewById(R.id.btn_confirm);
        ImageView imgError = view.findViewById(R.id.img_error);

        txtContent.setText(dialog_content);
        tvTitle.setText(title);
        imgError.setVisibility(View.GONE);

        btnConfirm.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);

                    if (dialog!=null){
                        dialog.dismiss();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        @SuppressWarnings("deprecation")
        int width = (int) (SystemUtils.getScreenWidth(context) * 0.8);
        lp.width = width;
        dialog.getWindow().setAttributes(lp);
        dialog.show();
    }

}
